/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.gr.uth.homework3injsp;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author mruiz
 */
public class XMLManager {

    private String filePath = "";

    public XMLManager(String filePath) {
        this.filePath = filePath;
    }

    public void createXML(String width, String height, String desc) {
        System.out.println("ok11");
        //   if(checkInputDataIsText(desc) && checkInputDataIsNumbers(height) && checkInputDataIsNumbers(width)) {

        Element image = new Element("image");
        Document doc = new Document(image);
        doc.setRootElement(image);

        Element e_width = new Element("width").setText(width);
        Element e_height = new Element("height").setText(height);
        Element e_desc = new Element("desc").setText(desc);



        doc.getRootElement().addContent(e_width);
        doc.getRootElement().addContent(e_height);
        doc.getRootElement().addContent(e_desc);


        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        try {
            xmlOutput.output(doc, new FileWriter(filePath));
        } catch (IOException ex) {
            Logger.getLogger(XMLManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
//        else{
//            IOException ex = new IOException();
//            Logger.getLogger(XMLManager.class.getName()).log(Level.SEVERE, "Invalid input data: " + width + height + desc, ex);
//            
//        }

    public void updateXML(String newWidth, String newHeigth, String newDesc) throws IOException, JDOMException {


        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File(filePath);

        Document doc = (Document) builder.build(xmlFile);
        Element rootNode = doc.getRootElement();


        rootNode.getChild("width").setText(newWidth);
        rootNode.getChild("height").setText(newHeigth);
        rootNode.getChild("desc").setText(newDesc);

        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        //xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, new FileWriter(filePath));

        // xmlOutput.output(doc, System.out);

    }

    public String[] getElements() throws JDOMException, IOException {

        String[] elements = new String[3];

        SAXBuilder builder = new SAXBuilder();

        File xmlFile = new File(filePath);


        Document doc = (Document) builder.build(xmlFile);
        Element rootNode = doc.getRootElement();


        elements[0] = rootNode.getChild("width").getText();
        elements[1] = rootNode.getChild("height").getText();
        elements[2] = rootNode.getChild("desc").getText();

        return elements;

    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    private boolean checkInputDataIsText(String text) {
        boolean isValid = false;
        String pattern = "[a-zA-Z0-9]*";
        if (text.matches(pattern)) {
            isValid = true;
        } else {
        }
        return isValid;
    }

    private boolean checkInputDataIsNumbers(String text) {
        boolean isValid = false;
        String pattern = "[0-9]*";
        if (text.matches(pattern)) {
            isValid = true;
        } else {
        }
        return isValid;
    }
}
